﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LTP.Common;

namespace TCS.Web.Admin.NewsClassManage
{
    public partial class AddR : System.Web.UI.Page
    {
        TCS.BLL.NewsManage.NewsClass bll = new TCS.BLL.NewsManage.NewsClass();
        public static string id = "1";
        private string toadd = "├";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
                {
                    id = Request.QueryString["id"].ToString();
                }
                DBindQY(int.Parse(id));
                LanM(1);
                if (Request.QueryString["tid"] != null && Request.QueryString["tid"] != "")
                {
                    DBind();
                }
                
            }
            
        }
        public void DBind()
        {

            TCS.Model.NewsManage.NewsClass model = bll.GetModel(int.Parse(Request.QueryString["tid"].ToString()));
            DBindQY(int.Parse(id));
            this.dropParent.SelectedValue = model.ParentId.ToString();
            this.txtClassDesc.Text = model.ClassDesc;//名称
            model.createtime = DateTime.Now;
            model.CATAPARENTID = 0;
            this.txt_pc.Text = model.CATASENDRATE;//频率
            this.txt_lx.Text = model.FEETYPE;//类型
            this.txt_stime.Text = model.BEGINDATE;//开始时间
            this.txt_endtime.Text = model.ENDDATE;//结束时间
            this.txt_bm.Text = model.BOSS;//编码
            this.txt_bh.Text = model.CATACODE;//编号
            this.txt_count.Text = model.MEMO;//概要说明
            this.txt_je.Text = model.FEEVALUE;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string ClassDesc = this.txtClassDesc.Text;           
            TCS.Model.NewsManage.NewsClass model = new TCS.Model.NewsManage.NewsClass();
            model.ClassDesc = ClassDesc;
            model.ClassPicture = "";
            model.ParentId = int.Parse(this.dropParent.SelectedValue);
            model.ClassId = int.Parse(id);//地区id
            model.createtime = DateTime.Now;
            model.CATAPARENTID = int.Parse(this.dropParent.SelectedValue);
            model.CATASENDRATE = this.txt_pc.Text;//频率
            model.FEETYPE = this.txt_lx.Text;//类型
            model.BEGINDATE = this.txt_stime.Text;//开始时间
            model.ENDDATE = this.txt_endtime.Text;//结束时间
            model.BOSS = this.txt_bm.Text;//编码
            model.CATACODE = this.txt_bh.Text;//编号
            model.MEMO = this.txt_count.Text;//概要说明
            model.FEEVALUE = this.txt_je.Text;

            TCS.BLL.NewsManage.NewsClass bll = new TCS.BLL.NewsManage.NewsClass();
            if (Request.QueryString["tid"] != null && Request.QueryString["tid"] != "")
            {
                model.CATAID=int.Parse(Request.QueryString["tid"].ToString());
                bll.Update(model);
                MessageBox.ShowAndRedirect(this, "修改成功！", "IndexR.aspx");
            }
            else
            {
                bll.Add(model);
                MessageBox.ShowAndRedirect(this, "添加成功！", "IndexR.aspx");
            }
        }

        private void DBindQY(int id)
        {
            string strWhere = "addid=" + id;
            DataTable dt = new TCS.BLL.Gsp_Administrative().GetListNew(1, strWhere, "addid").Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.labqy.Text = dt.Rows[0]["addName"].ToString();
            }
        }

        /// <summary>
        /// 栏目信息
        /// </summary>
        /// <param name="parentid"></param>
        private void LanM(int parentid)
        {

            DataSet ds = new TCS.BLL.NewsManage.NewsClass().GetClassFz(0, "cataparentid=" + parentid.ToString(), "cataid");
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    this.dropParent.Items.Add(new ListItem(toadd + "" + ds.Tables[0].Rows[i]["classdesc"].ToString(), ds.Tables[0].Rows[i]["cataid"].ToString()));
                    toadd += "─┴";
                    LanM(int.Parse(ds.Tables[0].Rows[i]["cataid"].ToString()));
                    toadd = toadd.Substring(0, toadd.Length - 2);
                }
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("indexR.aspx");
        }
    }
}
